package com.exercises;


class Exercise27 {
    public static void main(String[] args) {
        int[] arr = new int[]{3, 2, 2, 3};
        int result = removeElement(arr, 3);
        System.out.println(result);
        for (int i = 0; i < result; i++) {
            System.out.println(arr[i]);
        }

    }

//    public int removeElement(int[] nums, int val) {
//        int i = 0;
//        for (int j = 0; j < nums.length; j++) {
//            if (nums[j] != val) {
//                nums[i] = nums[j];
//                i++;
//            }
//        }
//        return i;
//    }


    public static int removeElement(int[] nums, int val) {
        int temp = 0, i = 0, j = nums.length - 1;
        while (i <= j) {
            if (nums[j] == val) {
                j--;
                continue;
            }
            if (nums[i] != val) {
                i++;
                continue;
            }

            temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            i++;
            j--;
        }
        return i > j ? i : 0;
    }

}

